package cheme.ProcessEditor.Policies;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.GroupRequest;

import cheme.ProcessEditor.Tools.CreateAfterRequest;

public class FieldPolicy extends ComponentEditPolicy
{
	@Override
	public EditPart getTargetEditPart(Request request)
	{
		if(request.getType().equals(REQ_MOVE))
		{
			return getHost().getParent();
		}
		
		return super.getTargetEditPart(request);
	}
	
	@Override
	public Command getCommand(Request request)
	{
		/*
		 * Fields have a few important requests that they forward on to thier 
		 * parent. 
		 */
		
		if(request instanceof CreateAfterRequest)
		{
			return getCreateAfterCmd((CreateAfterRequest)request);
		}
		else if(request.getType().equals(REQ_MOVE))
		{
			return getMoveCmd((ChangeBoundsRequest)request);
		}
		
		return super.getCommand(request);
	}
	
	private Command getMoveCmd(ChangeBoundsRequest request)
	{
		return null;
	}
	
	protected Command getCreateAfterCmd(CreateAfterRequest request)
	{
		
//		Give my parent a chance to create a new me after me.
		
//		If the request originated with this policies host, no need to
//		add it again.
		 
		if(!((CreateAfterRequest)request).getAfterList().contains(getHost()))
		{
			((CreateAfterRequest)request).addToAfterList(getHost());
		}
		
		return getHost().getParent().getCommand(request);
	}

	@Override
	protected Command getDeleteCommand(GroupRequest request)
	{
		/*
		 * My parent knows best how to delete me, the child. 
		 */
		
		return getHost().getParent().getCommand(request); 
	}
}
